package com.yanqu.road.server.manager.monopolymarket;

import com.yanqu.road.entity.config.battle.BattleResult;
import com.yanqu.road.entity.config.battle.PatronsBattleDetail;
import com.yanqu.road.entity.enums.eMonopolyMarketResType;
import com.yanqu.road.entity.monopolymarket.MonopolyMarket;
import com.yanqu.road.entity.monopolymarket.MonopolyMarketBeautyInfo;
import com.yanqu.road.entity.monopolymarket.MonopolyMarketNoticeData;
import com.yanqu.road.entity.monopolymarket.MonopolyMarketPatronsInfo;
import com.yanqu.road.entity.monopolymarket.config.MonopolyMarketResConfigData;
import com.yanqu.road.entity.monopolymarket.office.log.MonopolyMarketResGrabRobLogData;
import com.yanqu.road.entity.monopolymarket.resgrab.*;
import com.yanqu.road.entity.player.CrossUserBaseInfo;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.bussiness.monopolymarket.MonopolyMarketBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.BattlePb;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.monopolymarket.MonopolyMarketProto;
import com.yanqu.road.pb.patrons.PatronsProto;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.MonopolyMarketCommonMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;


/**
 * @Description 资源争夺战
 * @Author cwq
 * @Data 2021/8/16 10:58
 */
public class MonopolyMarketResGrabMgr {

    public static Logger logger = LogManager.getLogger(MonopolyMarketResGrabMgr.class.getName());

    public static AtomicLong maxResGrabFinishId;

    /**
     * 玩家采集完成的未领取奖励
     */
    public static Map<Long,List<MonopolyMarketResGrabFinishData>> userResGrabFinishMap = new ConcurrentHashMap<>();

    /**
     * 玩家采集完成的未加入排行榜的数值 （排行榜用） k：id
     */
    public static Map<Long,MonopolyMarketResGrabFinishData> userResGrabRankMap = new ConcurrentHashMap<>();

    /**
     * 临时采集的奖励 入库用
     */
    public static List<MonopolyMarketResGrabFinishData> tempResGrabFinishList = new ArrayList<>();

    /**
     * 配置  k：资源类型
     */
    public static HashMap<Integer,List<MonopolyMarketResConfigData>> resConfigListMap = new HashMap<>(2);

    /**
     * 配置  k：资源类型  k:level
     */
    public static Map<Integer, Map<Integer,MonopolyMarketResConfigData>> resConfigLevelMap = new HashMap<>(2);

    public static boolean reloadConfig(){
        HashMap<Integer,List<MonopolyMarketResConfigData>> tempResConfigListMap = new HashMap<>(2);
        Map<Integer, Map<Integer,MonopolyMarketResConfigData>> tempResConfigLevelMap = new HashMap<>(2);
        for (MonopolyMarketResConfigData item : MonopolyMarketMgr.resConfigList){
            if(item.getType() == eMonopolyMarketResType.RES_KING.getValue() ||  item.getType() == eMonopolyMarketResType.RES_QUEEN.getValue()){
                List<MonopolyMarketResConfigData> list = tempResConfigListMap.get(item.getType());
                if(list == null){
                    list = new ArrayList<>();
                    tempResConfigListMap.put(item.getType(),list);
                }
                list.add(item);

                Map<Integer,MonopolyMarketResConfigData> map = tempResConfigLevelMap.get(item.getType());
                if (map == null) {
                    map = new HashMap<>();
                    tempResConfigLevelMap.put(item.getType(),map);
                }
                map.put(item.getLevel(),item);
            }
        }
        resConfigListMap = tempResConfigListMap;
        resConfigLevelMap = tempResConfigLevelMap;
        return true;
    }

    /**
     * 初始化 资源争夺战
     * @param monopolyMarket
     */
    public static void initResGrab(MonopolyMarket monopolyMarket){
        int now = DateHelper.getCurrentSecond();
        for(MonopolyMarketResGrab item : monopolyMarket.monopolyMarketResGrab.values()){
            item.map = MonopolyMarketCommonMgr.initResGrabMap(item.type,monopolyMarket.group,resConfigListMap.get(item.type),MonopolyMarketMgr.crossConfig,false);
            // 加载数据库数据
            loadDbPointInfo(monopolyMarket,item);
            for(MonopolyMarketResGrabPointInfo pointInfo : item.pointsMap.values()){
                // 尝试结算 据点
                if(pointInfo.baseInfo.getUserId() > 0){
                    int settlementTime = getPointEndTime(item,pointInfo);
                    if(settlementTime != -1 && now >= settlementTime){
                        normalSettlement(monopolyMarket,item, pointInfo,false,settlementTime);
                    }else {
                        addCurPointInfo(item,pointInfo);
                    }
                }
            }
            // 公告
            int changTime = monopolyMarket.group.getChangTime();
            if(item.type == eMonopolyMarketResType.RES_KING.getValue()){
                changTime = Math.max(monopolyMarket.group.getKingCfgChangTime(),changTime);
            }else if(item.type == eMonopolyMarketResType.RES_QUEEN.getValue()){
                changTime = Math.max(monopolyMarket.group.getQueenCfgChangTime(),changTime);
            }
            List<MonopolyMarketNoticeData> list = MonopolyMarketBussiness.getMonopolyMarketNoticeDataList(MonopolyMarketMgr.getGroupId(monopolyMarket),item.type, changTime ,MonopolyMarketMgr.NOTICE_MAX_NUM);
            // 倒序
            Collections.reverse(list);
            monopolyMarket.noticeMap.put(item.type, list);
        }
    }

    /**
     * 进入资源资源争夺战 大厅
     */
    public static MonopolyMarketProto.MonopolyMarketEnterResGrabRespMsg.Builder enterResGrab(int type, List<MonopolyMarketProto.MonopolyMarketResGrabEnterParamEntity> pageParamList,long serverId,boolean isGetNotice) {
        MonopolyMarketProto.MonopolyMarketEnterResGrabRespMsg.Builder builder = MonopolyMarketProto.MonopolyMarketEnterResGrabRespMsg.newBuilder();
        MonopolyMarket monopolyMarket = MonopolyMarketMgr.getMonopolyMarket(serverId,true);
        if(monopolyMarket == null){
            builder.setRet(GameErrorCode.E_SYSTEM_NO_OPEN);
            return builder;
        }
        MonopolyMarketResGrab resGrab = monopolyMarket.monopolyMarketResGrab.get(type);
        if(resGrab == null){
            builder.setRet(GameErrorCode.E_MONOPOLY_MARKET_RES_GRAB_TYPE_NO_ERROR);
            return builder;
        }
        List<MonopolyMarketResGrabPointInfo> list = new ArrayList<>();
        // 计算边长最多有多少页
        int n = (resGrab.map.side + GameConfig.MONOPOLYMARKET_INITIAL_LOADNUM_2 - 1) / GameConfig.MONOPOLYMARKET_INITIAL_LOADNUM_2;
        for (MonopolyMarketProto.MonopolyMarketResGrabEnterParamEntity pageParam : pageParamList) {
            int x = pageParam.getPage() % n ;
            int y = pageParam.getPage() / n ;
            int startX = x * GameConfig.MONOPOLYMARKET_INITIAL_LOADNUM_2 ;
            int startY = y * GameConfig.MONOPOLYMARKET_INITIAL_LOADNUM_2 ;
            for (int i = 0; i < GameConfig.MONOPOLYMARKET_INITIAL_LOADNUM_2; i++) {
                for (int j = 0; j < GameConfig.MONOPOLYMARKET_INITIAL_LOADNUM_2; j++) {
                    int index = MonopolyMarketCommonMgr.calIndex(startX + j, startY + i,resGrab.map.side);
                    if(!isEmpty(resGrab,index)){
                        MonopolyMarketResGrabPointInfo pointInfo = resGrab.pointsMap.get(index);
                        if(pointInfo != null){
                            if(pageParam.getTime() == 0 || pageParam.getTime() < pointInfo.baseInfo.getChangeTime()){
                               list.add(pointInfo);
                            }
                        }
                    }
                }
            }
        }
        for(MonopolyMarketResGrabPointInfo item : list){
            builder.addPointList(MonopolyMarketResGrabMgr.builderMonopolyMarketResGrabPointEntity(item));
        }
        if(isGetNotice){
            synchronized (monopolyMarket){
                MonopolyMarketNoticeData noticeData = MonopolyMarketMgr.getNewMonopolyMarketNoticeData(monopolyMarket,type);
                if(noticeData != null){
                    builder.setNewNotice(MonopolyMarketMgr.builderMonopolyMarketNoticeEntity(noticeData));
                }
            }
        }
        builder.setTime(DateHelper.getCurrentSecond());
        builder.setRet(0);
        for (MonopolyMarketProto.MonopolyMarketResGrabEnterParamEntity paramEntity : pageParamList) {
            builder.addPageList(paramEntity.getPage());
        }
        return builder;
    }

    /**
     * 进入资源资源争夺战 据点
     */
    public static MonopolyMarketProto.MonopolyMarketEnterResGrabPointRespMsg.Builder enterResGrabPoint(long userId,long serverId, int type, int posIndex) {
        MonopolyMarketProto.MonopolyMarketEnterResGrabPointRespMsg.Builder builder = MonopolyMarketProto.MonopolyMarketEnterResGrabPointRespMsg.newBuilder();
        MonopolyMarket monopolyMarket = MonopolyMarketMgr.getMonopolyMarket(serverId,true);
        if(monopolyMarket == null){
            builder.setRet(GameErrorCode.E_SYSTEM_NO_OPEN);
            return builder;
        }
        MonopolyMarketResGrab resGrab = monopolyMarket.monopolyMarketResGrab.get(type);
        if(resGrab == null){
            builder.setRet(GameErrorCode.E_MONOPOLY_MARKET_RES_GRAB_TYPE_NO_ERROR);
            return builder;
        }
        if(isEmpty(resGrab,posIndex)){
            builder.setRet(GameErrorCode.E_MONOPOLY_MARKET_RES_GRAB_POINT_NO_EXIST);
            return builder;
        }
        synchronized (monopolyMarket){
            MonopolyMarketResGrabPointInfo pointInfo = getMonopolyMarketResGrabPointInfo(monopolyMarket,resGrab,posIndex,true);
            builder.setPointInfo(builderMonopolyMarketResGrabPointDetailEntity(pointInfo));
            pointInfo.userSet.add(userId);
            resGrab.userInPointMap.put(userId,posIndex);
        }
        builder.setRet(0);
        return builder;
    }

    /**
     * 资源争夺战 占领据点
     * @param userId
     * @param serverId
     * @param ctsMsg
     * @param userBaseInfo
     * @param objectInfoList
     * @return
     */
    public static MonopolyMarketProto.MonopolyMarketResGrabActionToGameServerMsg.Builder action(long userId, long serverId, MonopolyMarketProto.MonopolyMarketResGrabActionReqMsg ctsMsg,UserBaseInfo userBaseInfo,
            List<IMonopolyMarketObjectInfo> objectInfoList,int buffAddTimes ){
        MonopolyMarketProto.MonopolyMarketResGrabActionToGameServerMsg.Builder builder = MonopolyMarketProto.MonopolyMarketResGrabActionToGameServerMsg.newBuilder();
        builder.setActionType(ctsMsg.getActionType());
        builder.setType(ctsMsg.getType());
        builder.setPosIndex(ctsMsg.getPosIndex());
        MonopolyMarketProto.MonopolyMarketResGrabActionRespMsg.Builder stcMsg = MonopolyMarketProto.MonopolyMarketResGrabActionRespMsg.newBuilder();
        MonopolyMarket monopolyMarket = MonopolyMarketMgr.getMonopolyMarket(serverId,true);
        if(monopolyMarket == null){
            stcMsg.setRet(GameErrorCode.E_SYSTEM_NO_OPEN);
            builder.setStcMsg(stcMsg);
            return builder;
        }
        synchronized (monopolyMarket){
            if(!MonopolyMarketCommonMgr.isSystemOpen(monopolyMarket.group, ctsMsg.getType())){
                stcMsg.setRet(GameErrorCode.E_MONOPOLY_MARKET_SYSTEM_NO_OPEN);
                builder.setStcMsg(stcMsg);
                return builder;
            }
            MonopolyMarketResGrab resGrab = monopolyMarket.monopolyMarketResGrab.get(ctsMsg.getType());
            if(resGrab == null){
                stcMsg.setRet(GameErrorCode.E_MONOPOLY_MARKET_RES_GRAB_TYPE_NO_ERROR);
                builder.setStcMsg(stcMsg);
                return builder;
            }
            // 加个保护 如果今天未重置据点 不让操作
            int resGrabRefreshTime = 0;
            if(ctsMsg.getType() == eMonopolyMarketResType.RES_KING.getValue()){
                resGrabRefreshTime = monopolyMarket.group.getKingResGrabRefreshTime();
            }else if(ctsMsg.getType() == eMonopolyMarketResType.RES_QUEEN.getValue()){
                resGrabRefreshTime = monopolyMarket.group.getQueenResGrabRefreshTime();
            }
            if(DateHelper.getDiffDayNum(resGrabRefreshTime) != 0){
                stcMsg.setRet(GameErrorCode.E_MONOPOLY_MARKET_RES_GRAB_NO_REFRESH);
                builder.setStcMsg(stcMsg);
                return builder;
            }
            if(ctsMsg.getPosIndex() >= resGrab.map.indexLevelList.size()){
                stcMsg.setRet(GameErrorCode.E_MONOPOLY_MARKET_RES_GRAB_POINT_NO_EXIST);
                builder.setStcMsg(stcMsg);
                return builder;
            }
            MonopolyMarketResConfigData configData = getMonopolyMarketResConfigData(resGrab, ctsMsg.getPosIndex());
            if(configData == null){
                stcMsg.setRet(GameErrorCode.E_MONOPOLY_MARKET_OFFICE_SEAT_CONFIG_NO_EXIST);
                builder.setStcMsg(stcMsg);
                return builder;
            }
            MonopolyMarketResGrabPointInfo pointInfo = getMonopolyMarketResGrabPointInfo(monopolyMarket,resGrab,ctsMsg.getPosIndex(),true);
            if(pointInfo.baseInfo.getSeatTime() != ctsMsg.getSeatTime() || pointInfo.baseInfo.getUserId() != ctsMsg.getSeatUserId()){
                stcMsg.setRet(GameErrorCode.E_MONOPOLY_MARKET_RES_GRAB_SEAT_INFO_CHANGE);
                builder.setStcMsg(stcMsg);
                return builder;
            }
            int now = DateHelper.getCurrentSecond();
            if(pointInfo.baseInfo.getSeatTime() > 0 && now >= pointInfo.baseInfo.getEndTime()){
                stcMsg.setRet(GameErrorCode.E_MONOPOLY_MARKET_RES_GRAB_FINISH);
                builder.setStcMsg(stcMsg);
                return builder;
            }
            boolean isChange = false;
            MonopolyMarketResGrabRobLogData robLogData = new MonopolyMarketResGrabRobLogData();

            switch (ctsMsg.getActionType()){
                case 0:{
                    // 不能重复入座
                    if(pointInfo.baseInfo.getUserId() == userId){
                        stcMsg.setRet(GameErrorCode.E_MONOPOLY_MARKET_NO_CAN_SEATED_REPEATED_POSITION);
                        builder.setStcMsg(stcMsg);
                        return builder;
                    }
                    // 多判断一次 怕跨服消息还没回给区服 立马再派遣一次
                    if(isRepeatDispatch(resGrab,userId,objectInfoList)){
                        stcMsg.setRet(GameErrorCode.E_MONOPOLY_MARKET_RES_GRAB_OBJECT_ID_IS_DUPLICATE);
                        builder.setStcMsg(stcMsg);
                        return builder;
                    }
                    boolean success = false;
                    long defenderId = pointInfo.baseInfo.getUserId();
                    UserBaseInfo defenderInfo = CrossUserMgr.getUserBaseInfo(defenderId);
                    // 鼓舞加成
                    int buffValues = 0;
                    if(ctsMsg.getType() == eMonopolyMarketResType.RES_KING.getValue()){
                        buffValues = GameConfig.MONOPOLYMARKET_BUFF_ADDITION_2 * buffAddTimes;
                    }else if(ctsMsg.getType() == eMonopolyMarketResType.RES_QUEEN.getValue()){
                        buffValues = GameConfig.MONOPOLYMARKET_BUFF_ADDITION_3 * buffAddTimes;
                    }
                    List<IMonopolyMarketObjectInfo> defendObjectList = null;
                    int defendBuffValues = 0;
                    if(defenderId == 0){
                        // 打npc
                        defendObjectList = getNpcObjectList(monopolyMarket,configData);
                    }else {
                        defendObjectList = pointInfo.baseInfo.getObjectInfoList();
                        defendBuffValues = pointInfo.baseInfo.getBuffValues();
                    }
                    success = battle(pointInfo,userId,objectInfoList,stcMsg,buffValues,configData,defendObjectList);
                    robLogData.setObjectStr(getObjectStr(defendObjectList));
                    robLogData.setValue(calPointValue(defendObjectList,defendBuffValues));
                    if(defenderId == 0) {
                        // 需要攻打 npc
                        robLogData.setUserId(0);
                        robLogData.setRemainTime(pointInfo.baseInfo.getRemainTime());
                        stcMsg.setPointStatus(0);
                    }else {
                        robLogData.setUserId(pointInfo.baseInfo.getUserId());
                        int rewardNum = 0;
                        int totalRewardTime = now - pointInfo.baseInfo.getSeatTime();
                        // 计算掠夺资源
                        if(success){
                            // 上次采集到结束的收益
                            rewardNum = calRewardNum(monopolyMarket,pointInfo.baseInfo.getProduceRewardLastTime(),now,configData);
                            int rewardTime = now - pointInfo.baseInfo.getProduceRewardLastTime();
                            // 总采集的资源
                            int totalRewardNum = calRewardNum(monopolyMarket,pointInfo.baseInfo.getSeatTime(),now,configData);

                            // 保存被抢记录并通知区服有奖励可领
                            saveFinishRecord(resGrab,pointInfo,configData,userBaseInfo.getNickName(),rewardNum,totalRewardNum,totalRewardTime,now,rewardTime,pointInfo.baseInfo.getProduceRewardLastTime());
                            // 离开位置
                            leavePointInfo(resGrab,pointInfo,now);
                            robLogData.setRewardNum(totalRewardNum);
                            robLogData.setRemainTime(pointInfo.baseInfo.getRemainTime());
                        }else {
                            robLogData.setRemainTime(pointInfo.baseInfo.getRemainTime() - totalRewardTime);
                        }
                        addResGrabReport(resGrab,userId,userBaseInfo,pointInfo.baseInfo.getPosIndex(),defenderInfo.getServerId(),defenderId,success,configData.getLevel(),rewardNum);
                        stcMsg.setPointStatus(1);
                    }
                    stcMsg.setIsSuccess(success);
                    if(success){
                        // 入座成功收益
                        int cd = MonopolyMarketCommonMgr.getResGrabPointCD(ctsMsg.getType());
                        int rate = MonopolyMarketCommonMgr.calResRate(monopolyMarket.group.getServerList().size(),configData.rewardValue1,configData.rewardAdd1,configData.getId(),MonopolyMarketMgr.crossConfig,1);
                        int rewardNum = (rate * 60 * configData.getSeize() + cd - 1)/ cd ;
                        stcMsg.setRewardNum(rewardNum);
                        builder.setRewardNum(rewardNum);
                        if(configData.getIsad() == 1){
                            // 加公告
                            MonopolyMarketMgr.addNotice(monopolyMarket,resGrab.type,userId,defenderId,pointInfo.baseInfo.getPosIndex());
                        }
                        // 占领据点
                        occupyPoint(resGrab,userId, now, pointInfo,objectInfoList,buffValues);
                        CrossUserMgr.updateUserBaseInfo(userId,userBaseInfo);
                        stcMsg.setPointInfo(builderMonopolyMarketResGrabPointDetailEntity(pointInfo));
                        robLogData.setSitReward(rewardNum);
                    }
                    isChange = success;
                    stcMsg.addAllObjectList(ctsMsg.getObjectIdList());

                    robLogData.setIsWin(success);
                    robLogData.setRobId(userId);
                    robLogData.setRobObjectStr(getObjectStr(objectInfoList));
                    robLogData.setRobValue(calPointValue(objectInfoList,buffValues));
                }
                break;
                case 1:{
                    // 撤回
                    if(pointInfo.baseInfo.getUserId() != userId){
                        stcMsg.setRet(GameErrorCode.E_MONOPOLY_MARKET_RES_GRAB_IS_NOT_CURRENTLY_IN_THIS_SEAT);
                        builder.setStcMsg(stcMsg);
                        return builder;
                    }
                    // 结算
                    robLogData.setUserId(pointInfo.baseInfo.getUserId());
                    robLogData.setObjectStr(getObjectStr(pointInfo.baseInfo.getObjectInfoList()));
                    robLogData.setValue(calPointValue(pointInfo.baseInfo.getObjectInfoList(),pointInfo.baseInfo.getBuffValues()));
                    robLogData.setRemainTime(pointInfo.baseInfo.getRemainTime());

                    MonopolyMarketProto.MonopolyMarketSynPointSettlementToGameServerMsg.Builder settlement = normalSettlement(monopolyMarket,resGrab,pointInfo,true, now);
                    builder.setSettlement(settlement);
                    isChange = true;
                    stcMsg.setPointInfo(builderMonopolyMarketResGrabPointDetailEntity(pointInfo));
                    robLogData.setRewardNum(settlement.getRewardNum());
                    builder.setRewardNum(settlement.getRewardNum());
                }
                break;
                default:{
                    stcMsg.setRet(GameErrorCode.E_MONOPOLY_MARKET_RES_GRAB_TYPE_ERROR);
                    builder.setStcMsg(stcMsg);
                    return builder;
                }
            }
            if(isChange){
                broadcastMonopolyMarketResgrabRespMsg(pointInfo,userId);
            }
            // 加日志
            robLogData.setGroupId(monopolyMarket.group.getGroupId());
            robLogData.setPosIndex(ctsMsg.getPosIndex());
            robLogData.setSide(resGrab.map.side);
            robLogData.setLevel(configData.getLevel());
            robLogData.setType(configData.getType());
            robLogData.setOccupyStr(getCurUserPoint(resGrab,userId));
            robLogData.setLogTime(now);
            MonopolyMarketLogMgr.addMonopolyMarketResGrabRobLogData(robLogData);
        }
        stcMsg.setRet(0);
        builder.setStcMsg(stcMsg);
        return builder;
    }


    /**
     * 批量领取据点奖励
     * @param userId
     * @param serverId
     * @param list
     * @return
     */
    public static MonopolyMarketProto.MonopolyMarketGetPointRewardToGameServerMsg.Builder getPointReward(long userId,long serverId, List<MonopolyMarketProto.MonopolyMarketGetPointRewardTemp> list){
        MonopolyMarketProto.MonopolyMarketGetPointRewardToGameServerMsg.Builder resp = MonopolyMarketProto.MonopolyMarketGetPointRewardToGameServerMsg.newBuilder();
        MonopolyMarketProto.MonopolyMarketGetPointRewardRespMsg.Builder stcMsg = MonopolyMarketProto.MonopolyMarketGetPointRewardRespMsg.newBuilder();
        MonopolyMarket monopolyMarket = MonopolyMarketMgr.getMonopolyMarket(serverId,true);
        if(monopolyMarket == null){
            stcMsg.setRet(GameErrorCode.E_SYSTEM_NO_OPEN);
            resp.setStcMsg(stcMsg);
            return resp;
        }
        synchronized (monopolyMarket){
            Property property = new Property();
            for (MonopolyMarketProto.MonopolyMarketGetPointRewardTemp item : list){
                // 领取奖励
                MonopolyMarketResGrab resGrab = monopolyMarket.monopolyMarketResGrab.get(item.getType());
                if(resGrab != null){
                    int totalRewardNum = 0;
                    Set<MonopolyMarketResGrabPointInfo> pointInfos = resGrab.getUserPointSet(userId,false);
                    if(pointInfos != null){
                        for (MonopolyMarketResGrabPointInfo pointInfo : pointInfos){
                            // 加个保护
                            if(pointInfo.baseInfo.getUserId() != userId){
                                continue;
                            }
                            // 玩家正在采集的据点
                            if(item.getPosIndexList().contains(pointInfo.baseInfo.getPosIndex())){
                                // 判断是否能够被领取
                                MonopolyMarketResGrabReceiveResult receiveResult = receivePoint(monopolyMarket,resGrab,pointInfo);
                                if(receiveResult.getAddValue() > 0){
                                    totalRewardNum += receiveResult.getAddValue();
                                    resp.addAddRankList(builderMonopolyMarketAddRankValueEntity(receiveResult));
                                }
                                stcMsg.addTeamList(builderMonopolyMarketGetTeamEntity(pointInfo));
                            }
                        }
                    }
                    if(totalRewardNum > 0){
                        int goodsId = MonopolyMarketCommonMgr.getGoodsId(item.getType());
                        property.addProperty(new Property(goodsId,BigInteger.valueOf(totalRewardNum)));
                    }
                }
            }
            if(property.getGoods().size() > 0){
                stcMsg.setRewards(PropertyHelper.parsePropertyToString(property));
            }
        }
        stcMsg.setRet(0);
        resp.setStcMsg(stcMsg);
        return resp;
    }

    /**
     * 搜索
     * @param serverId
     * @param type
     * @param level
     * @param startPosIndex  不包含
     * @return
     */
    public static MonopolyMarketProto.MonopolyMarketResGrabSearchRespMsg.Builder search(long serverId, int type, int level, int startPosIndex,boolean isEmpty){
        MonopolyMarketProto.MonopolyMarketResGrabSearchRespMsg.Builder builder = MonopolyMarketProto.MonopolyMarketResGrabSearchRespMsg.newBuilder();
        MonopolyMarket monopolyMarket = MonopolyMarketMgr.getMonopolyMarket(serverId,true);
        if(monopolyMarket == null){
            builder.setRet(GameErrorCode.E_SYSTEM_NO_OPEN);
            return builder;
        }
        synchronized (monopolyMarket) {
            MonopolyMarketResGrab resGrab = monopolyMarket.monopolyMarketResGrab.get(type);
            if (resGrab == null) {
                builder.setRet(GameErrorCode.E_MONOPOLY_MARKET_RES_GRAB_TYPE_NO_ERROR);
                return builder;
            }
            List<Integer> indexList = resGrab.map.levelIndexListMap.get(level);
            for (Integer index : indexList){
                if(startPosIndex >= index){
                    continue;
                }
                MonopolyMarketResGrabPointInfo pointInfo = getMonopolyMarketResGrabPointInfo(monopolyMarket,resGrab,index,true);
                if(pointInfo.baseInfo.getRemainTime() == 0){
                    // 已采集完不下发
                    continue;
                }
                if(!isEmpty && pointInfo.baseInfo.getUserId() != 0){
                    // 显示正在采集的据点
                    builder.addPointList(builderMonopolyMarketResGrabSearchPointEntity(pointInfo));
                }else if (isEmpty && pointInfo.baseInfo.getUserId() == 0){
                    // 显示 无人 可采集的据点
                    builder.addPointList(builderMonopolyMarketResGrabSearchPointEntity(pointInfo));
                }

                if(builder.getPointListCount() >= GameConfig.MONOPOLYMARKET_RES_GRAB_SEARCH_PAGE_NUM){
                    break;
                }
            }
        }
        builder.setRet(0);
        return builder;
    }



    /**
     * 领取据点奖励
     * @param pointInfo
     * @return
     */
    private static MonopolyMarketResGrabReceiveResult receivePoint(MonopolyMarket monopolyMarket,MonopolyMarketResGrab resGrab, MonopolyMarketResGrabPointInfo pointInfo){
        MonopolyMarketResGrabReceiveResult receiveResult = new MonopolyMarketResGrabReceiveResult();
        int now = DateHelper.getCurrentSecond();
        int rewardNum = 0;
        int pointEndTime = getPointEndTime(resGrab,pointInfo);
        if((now >= (pointInfo.baseInfo.getReceiveLastTime() + GameConfig.MONOPOLYMARKET_GETREWARD_CD) && (pointInfo.baseInfo.getProduceRewardLastTime() > 0) && pointEndTime >= now)){
            // 表示领奖cd到了可以领奖，但不一定有奖励可以领
            MonopolyMarketResConfigData configData = MonopolyMarketResGrabMgr.getMonopolyMarketResConfigData(resGrab,pointInfo.baseInfo.getPosIndex());
            int rewardTime = now - pointInfo.baseInfo.getProduceRewardLastTime();
            // 采集单位数量
            int cd = MonopolyMarketCommonMgr.getResGrabPointCD(resGrab.type);
            int i = rewardTime / cd;
            if(i > 0){
                // 总采集的资源
                int rate = MonopolyMarketCommonMgr.calResRate(monopolyMarket.group.getServerList().size(),configData.rewardValue1,configData.rewardAdd1,configData.getId(),MonopolyMarketMgr.crossConfig,1);
                rewardNum = i * rate;

                receiveResult.setAddValue(rewardNum);
                receiveResult.setStartTime(pointInfo.baseInfo.getProduceRewardLastTime());
                receiveResult.setTime(rewardTime);
                receiveResult.setType(resGrab.type);

                pointInfo.baseInfo.setReceiveLastTime(now);
                pointInfo.baseInfo.setProduceRewardLastTime(pointInfo.baseInfo.getProduceRewardLastTime() + i * cd);
                synchronized (resGrab.updateAndAddPointInfoSet){
                    resGrab.updateAndAddPointInfoSet.add(pointInfo);
                }
                broadcastMonopolyMarketResgrabRespMsg(pointInfo,pointInfo.baseInfo.getUserId());
            }
        }
        return receiveResult;
    }


    /**
     * 正常采集据点结算  不包括抢夺
     * @param pointInfo
     */
    private static MonopolyMarketProto.MonopolyMarketSynPointSettlementToGameServerMsg.Builder normalSettlement(MonopolyMarket monopolyMarket,MonopolyMarketResGrab resGrab, MonopolyMarketResGrabPointInfo pointInfo,boolean recall, int endTime){
        MonopolyMarketResConfigData configData = getMonopolyMarketResConfigData(resGrab, pointInfo.baseInfo.getPosIndex());
        MonopolyMarketProto.MonopolyMarketSynPointSettlementToGameServerMsg.Builder settlementMsg = null;
        // 上次采集到结束的收益
        int rewardNum = calRewardNum(monopolyMarket,pointInfo.baseInfo.getProduceRewardLastTime(),endTime,configData);
        int rewardTime = endTime - pointInfo.baseInfo.getProduceRewardLastTime();
        // 总采集的资源
        int totalRewardTime = endTime - pointInfo.baseInfo.getSeatTime();
        int totalRewardNum = calRewardNum(monopolyMarket,pointInfo.baseInfo.getSeatTime(),endTime,configData);
        if(recall){
            // 玩家撤回主动推给区服结算
            settlementMsg = MonopolyMarketProto.MonopolyMarketSynPointSettlementToGameServerMsg.newBuilder();
            settlementMsg.setType(resGrab.type);
            settlementMsg.setPosIndex(pointInfo.baseInfo.getPosIndex());
            settlementMsg.setSide(resGrab.map.side);
            settlementMsg.setLevel(configData.getLevel());
            settlementMsg.setRewardTime(totalRewardTime);
            settlementMsg.setRewardNum(rewardNum);
            settlementMsg.addAllDispatchObjectId(pointInfo.objectInfoMap.keySet());
            settlementMsg.setBuffValues(pointInfo.baseInfo.getBuffValues());
            settlementMsg.setTotalRewardNum(totalRewardNum);
        }else {
            saveFinishRecord(resGrab,pointInfo,configData,"",rewardNum,totalRewardNum,totalRewardTime,endTime,rewardTime,pointInfo.baseInfo.getProduceRewardLastTime());
        }
        // 离开位置
        leavePointInfo(resGrab,pointInfo,endTime);
        return settlementMsg;
    }

    /**
     * 采集收益计算
     * @param startTime
     * @param endTime
     * @param configData
     * @return
     */
    public static int calRewardNum(MonopolyMarket monopolyMarket,int startTime, int endTime,MonopolyMarketResConfigData configData){
        int rewardTime = endTime - startTime;
        // 采集单位数量
        int i = MonopolyMarketResGrabMgr.calResProduceTime(configData.getType(),rewardTime);
        // 总采集的资源
        int rewardNum = i * MonopolyMarketCommonMgr.calResRate(monopolyMarket.group.getServerList().size(),configData.rewardValue1,configData.rewardAdd1,configData.getId(),MonopolyMarketMgr.crossConfig,1);
        return Math.max(rewardNum,0);
    }

    /**
     * 抢夺成功后结算
     * @param resGrab
     * @param pointInfo
     * @param configData
     * @param robName
     * @param rewardNum
     * @param totalRewardNum
     * @param totalRewardTime
     * @param endTime
     * @param totalCalTime 该记录总采集时长
     * @param startCalTime 该记录开始计算的时间
     */
    private static void saveFinishRecord(MonopolyMarketResGrab resGrab, MonopolyMarketResGrabPointInfo pointInfo, MonopolyMarketResConfigData configData,String robName,int rewardNum,int totalRewardNum, int totalRewardTime, int endTime,int totalCalTime,int startCalTime){
            // 被动结算 保存记录 通知区服有结算
        MonopolyMarketResGrabFinishData finishData = new MonopolyMarketResGrabFinishData();
        finishData.setId(MonopolyMarketResGrabMgr.maxResGrabFinishId.incrementAndGet());
        finishData.setType(pointInfo.baseInfo.getType());
        finishData.setUserId(pointInfo.baseInfo.getUserId());
        finishData.setPosIndex(pointInfo.baseInfo.getPosIndex());
        finishData.setSide(resGrab.map.side);
        finishData.setLevel((byte)(configData.getLevel()));
        finishData.setRobName(robName);
        finishData.setObjectStr(getObjectStr(pointInfo.baseInfo.getObjectInfoList()));
        finishData.setRewardTime(totalRewardTime);
        finishData.setRewardNum(rewardNum);
        finishData.setTotalRewardNum(totalRewardNum);
        finishData.setTotalTime(totalCalTime);
        finishData.setStartTime(startCalTime);
        finishData.setBuffValues(pointInfo.baseInfo.getBuffValues());
        finishData.setTime(endTime);
        finishData.setInsertOption();
        if(finishData.getRewardNum() > 0){
            synchronized (MonopolyMarketResGrabMgr.userResGrabRankMap){
                MonopolyMarketResGrabMgr.userResGrabRankMap.put(finishData.getId(),finishData);
            }
        }else {
            finishData.setIsJoinRank(true);
        }
        List<MonopolyMarketResGrabFinishData> list = getMonopolyMarketResGrabFinishDataList(pointInfo.baseInfo.getUserId(),true);
        synchronized (list){
            list.add(finishData);
        }
        synchronized (tempResGrabFinishList) {
            tempResGrabFinishList.add(finishData);
        }
    }

    /**
     * 获取玩家正在采集的据点
     * @param type
     * @param serverId
     *
     * @param id
     * @return
     */
    public static MonopolyMarketProto.MonopolyMarketResGrabGetUserPointRespMsg.Builder getUserPoint(int type,int resType, long serverId,long id){
        MonopolyMarketProto.MonopolyMarketResGrabGetUserPointRespMsg.Builder builder = MonopolyMarketProto.MonopolyMarketResGrabGetUserPointRespMsg.newBuilder();
        MonopolyMarket monopolyMarket = MonopolyMarketMgr.getMonopolyMarket(serverId,true);
        if(monopolyMarket == null){
            builder.setRet(GameErrorCode.E_SYSTEM_NO_OPEN);
            return builder;
        }
        long targetUserId = 0;
        UserBaseInfo targetUserInfo = null;
        if(type != 0){
            // 短编号查询
            Map<Long, CrossUserBaseInfo> map = CrossUserMgr.getUserAliasIdUserBaseInfoMap(serverId,false);
            if(map == null){
                builder.setRet(GameErrorCode.E_MONOPOLY_MARKET_RES_GRAB_THIS_USER_NO_POINT);
                return builder;
            }
            CrossUserBaseInfo crossUserBaseInfo = map.get(id);
            if(crossUserBaseInfo == null){
                builder.setRet(GameErrorCode.E_MONOPOLY_MARKET_RES_GRAB_THIS_USER_NO_POINT);
                return builder;
            }
            targetUserInfo = crossUserBaseInfo.getUserBaseInfo();
            targetUserId = crossUserBaseInfo.getUserId();
        }else {
            targetUserId = id;
            targetUserInfo = CrossUserMgr.getUserBaseInfo(targetUserId);
            if(targetUserInfo == null){
                builder.setRet(GameErrorCode.E_MONOPOLY_MARKET_RES_GRAB_THIS_USER_NO_POINT);
                return builder;
            }
        }
        synchronized (monopolyMarket) {
            List<MonopolyMarketResGrab> list = new ArrayList<>();
            if(resType == 0){
                list.addAll(monopolyMarket.monopolyMarketResGrab.values());
            }else {
                MonopolyMarketResGrab resGrab = monopolyMarket.monopolyMarketResGrab.get(resType);
                if(resGrab != null){
                    list.add(resGrab);
                }
            }
            for (MonopolyMarketResGrab resGrab : list){
                Set<MonopolyMarketResGrabPointInfo> set = resGrab.getUserPointSet(targetUserId,false);
                if(set != null){
                    for (MonopolyMarketResGrabPointInfo item : set){
                        if(item.baseInfo.getUserId() != targetUserId){
                            continue;
                        }
                        builder.addUserPointList(builderMonopolyMarketResGrabUserPointEntity(item));
                    }
                }
                builder.setUserId(targetUserId);
                builder.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(targetUserInfo));
            }
        }
        builder.setRet(0);
        return builder;
    }


    /**
     * @param monopolyMarket
     */
    public static void save(MonopolyMarket monopolyMarket){
        List<MonopolyMarketResGrabPointInfo> pointInfoTempList = new ArrayList<>();
        for (MonopolyMarketResGrab item : monopolyMarket.monopolyMarketResGrab.values()){
            synchronized (item.updateAndAddPointInfoSet){
                pointInfoTempList.addAll(item.updateAndAddPointInfoSet);
                item.updateAndAddPointInfoSet.clear();
            }
        }
        List<MonopolyMarketResGrabPointData> updatePointInfoList = new ArrayList<>();
        List<MonopolyMarketResGrabPointData> addPointInfoList = new ArrayList<>();
        for (MonopolyMarketResGrabPointInfo pointInfo : pointInfoTempList){
            if (pointInfo.baseInfo.isUpdateOption()) {
                updatePointInfoList.add(pointInfo.baseInfo);
            } else if (pointInfo.baseInfo.isInsertOption()) {
                addPointInfoList.add(pointInfo.baseInfo);
            }
        }
        if(addPointInfoList.size() > 0){
            MonopolyMarketBussiness.addMonopolyMarketResGrabPointDataList(addPointInfoList);
        }
        if(updatePointInfoList.size() > 0){
            MonopolyMarketBussiness.updateMonopolyMarketResGrabPointDataList(updatePointInfoList);
        }
    }

    /**
     * 清空位置信息
     * @param resGrab
     * @param pointInfo
     */
    public static void clearPointInfo(MonopolyMarketResGrab resGrab,MonopolyMarketResGrabPointInfo pointInfo,boolean isCalRemainTime){
        pointInfo.baseInfo.setUserId(0);
        if(isCalRemainTime){
            pointInfo.baseInfo.setRemainTime(calResTotalTime(resGrab,pointInfo.baseInfo.getPosIndex()));
        }else {
            pointInfo.baseInfo.setRemainTime(0);
        }
        pointInfo.baseInfo.setReceiveLastTime(0);
        pointInfo.baseInfo.setProduceRewardLastTime(0);
        pointInfo.baseInfo.setSeatTime(0);
        pointInfo.baseInfo.setEndTime(0);
        pointInfo.baseInfo.setBuffValues(0);
        pointInfo.baseInfo.getObjectInfoList().clear();
        pointInfo.objectInfoMap.clear();
        pointInfo.userSet.clear();
        pointInfo.baseInfo.setChangeTime(0);
    }

    /**
     * 重置资源战 加载
     * @param monopolyMarket
     * @param resGrab
     */
    public static void resetResGrab(MonopolyMarket monopolyMarket,MonopolyMarketResGrab resGrab,boolean isForceSettlement){
        // 强制结算采集中的据点
        if(isForceSettlement){
            MonopolyMarketResGrabMgr.forceSettlement(monopolyMarket,resGrab);
        }
        resGrab.minResGrabEndTime = 0;
        // 清空原来的数据
        for (MonopolyMarketResGrabPointInfo pointInfo : resGrab.pointsMap.values()){
            clearPointInfo(resGrab,pointInfo,false);
        }
        resGrab.updateAndAddPointInfoSet.addAll(resGrab.pointsMap.values());

        resGrab.map = MonopolyMarketCommonMgr.initResGrabMap(resGrab.type,monopolyMarket.group,resConfigListMap.get(resGrab.type),MonopolyMarketMgr.crossConfig,false);
        // 加载数据库数据 1先保留原来的数据 2 在加载出未有的数据
        ConcurrentHashMap<Integer,MonopolyMarketResGrabPointInfo> newMap = new ConcurrentHashMap<>();
        Map<Integer,MonopolyMarketResGrabPointData> pointMap = MonopolyMarketBussiness.getMonopolyMarketResGrabPointDataMap(monopolyMarket.group.getGroupId(), resGrab.type);

        for(Map.Entry<Integer,Integer> entry : resGrab.map.indexLevelMap.entrySet()){
            if(entry.getValue() > 0){
                int posIndex = entry.getKey();
                MonopolyMarketResGrabPointInfo pointInfo = resGrab.pointsMap.get(posIndex);
                if(pointInfo == null){
                    // 未被加载
                    MonopolyMarketResGrabPointData pointData = pointMap.get(posIndex);
                    if(pointData != null){
                        pointInfo = new MonopolyMarketResGrabPointInfo(pointData);
                    }
                }
                if(pointInfo != null){
                    newMap.put((pointInfo.baseInfo.getPosIndex()),pointInfo);
                }
            }
        }
        for (MonopolyMarketResGrabPointInfo pointInfo : newMap.values()){
            clearPointInfo(resGrab,pointInfo,true);
        }
        resGrab.updateAndAddPointInfoSet.addAll(newMap.values());

        resGrab.pointsMap = newMap;
        resGrab.userInPointMap.clear();
        resGrab.minResGrabEndTime = 0;
        resGrab.collectingPointInfoList.clear();
        resGrab.userPointMap.clear();
        MonopolyMarketMgr.clearNotice(monopolyMarket,resGrab.type);
    }

    /**
     * 关闭资源战
     * @param monopolyMarket
     * @param resGrab
     */
    public static void closeResGrab(MonopolyMarket monopolyMarket,MonopolyMarketResGrab resGrab){
        // 强制结算采集中的据点
        MonopolyMarketResGrabMgr.forceSettlement(monopolyMarket,resGrab);
        resGrab.minResGrabEndTime = 0;
        // 清空原来的数据
        for (MonopolyMarketResGrabPointInfo pointInfo : resGrab.pointsMap.values()){
            clearPointInfo(resGrab,pointInfo,false);
        }
        resGrab.updateAndAddPointInfoSet.addAll(resGrab.pointsMap.values());

        resGrab.userInPointMap.clear();
        resGrab.minResGrabEndTime = 0;
        resGrab.collectingPointInfoList.clear();
        MonopolyMarketMgr.clearNotice(monopolyMarket,resGrab.type);
    }


    public static MonopolyMarketResGrabPointInfo getMonopolyMarketResGrabPointInfo(MonopolyMarket monopolyMarket,MonopolyMarketResGrab resGrab,int posIndex, boolean newIfNull){
        MonopolyMarketResGrabPointInfo pointInfo = resGrab.pointsMap.get(posIndex);
        if(pointInfo == null && newIfNull){
            MonopolyMarketResGrabPointData pointData = new MonopolyMarketResGrabPointData();
            pointData.setInsertOption();
            pointData.setGroupId((int)MonopolyMarketMgr.getGroupId(monopolyMarket));
            pointData.setType((byte) resGrab.type);
            pointData.setPosIndex(posIndex);
            pointData.setRemainTime(calResTotalTime(resGrab, posIndex));
            pointData.setChangeTime(DateHelper.getCurrentSecond());
            pointInfo = new MonopolyMarketResGrabPointInfo(pointData);
            resGrab.pointsMap.put(posIndex,pointInfo);
            synchronized (resGrab.updateAndAddPointInfoSet){
                resGrab.updateAndAddPointInfoSet.add(pointInfo);
            }
        }
        return pointInfo;
    }


    /**
     * 计算资源 总采集时长
     * @return
     */
    private static int calResTotalTime(MonopolyMarketResGrab resGrab, int posIndex){
        MonopolyMarketResConfigData configData = getMonopolyMarketResConfigData(resGrab, posIndex);
        return (int)(configData.getLimit() * DateHelper.MINUTE_SECONDS);
    }

    /**
     * 根据索引位置获取当前配置的等级  配置
     * @param resGrab
     * @param posIndex
     * @return
     */
    private static MonopolyMarketResConfigData getMonopolyMarketResConfigData(MonopolyMarketResGrab resGrab, int posIndex){
        int level = resGrab.map.indexLevelMap.get(posIndex);
        Map<Integer,MonopolyMarketResConfigData> map = resConfigLevelMap.get(resGrab.type);
        return map.get(level);
    }

    private static int calResProduceTime(int type ,int time){
        int cd = MonopolyMarketCommonMgr.getResGrabPointCD(type);
        return time / cd;
    }

    /**
     * 是否是空据点
     * @return
     */
    public static boolean isEmpty(MonopolyMarketResGrab resGrab,int posIndex){
        if(resGrab.map.indexLevelList.size() <= posIndex ){
            return true;
        }
        Integer level = resGrab.map.indexLevelList.get(posIndex);
        if(level == 0){
            return true;
        }
        return false;
    }

    /**
     * 数据库加载位置信息
     * @param monopolyMarket
     */
    private static void loadDbPointInfo(MonopolyMarket monopolyMarket,MonopolyMarketResGrab resGrab){
        Map<Integer,MonopolyMarketResGrabPointData> pointMap = MonopolyMarketBussiness.getMonopolyMarketResGrabPointDataMap(monopolyMarket.group.getGroupId(), resGrab.type);
        for(MonopolyMarketResGrabPointData item : pointMap.values()){
            if(resGrab.map.indexLevelMap.getOrDefault(item.getPosIndex(),0) > 0){
                MonopolyMarketResGrabPointInfo pointInfo = new MonopolyMarketResGrabPointInfo(item);
                resGrab.pointsMap.put(pointInfo.baseInfo.getPosIndex() ,pointInfo);
            }
        }
    }

    public static MonopolyMarketProto.MonopolyMarketResGrabPointDetailEntity.Builder builderMonopolyMarketResGrabPointDetailEntity(MonopolyMarketResGrabPointInfo pointInfo){
        MonopolyMarketProto.MonopolyMarketResGrabPointDetailEntity.Builder builder = MonopolyMarketProto.MonopolyMarketResGrabPointDetailEntity.newBuilder();
        builder.setPosIndex(pointInfo.baseInfo.getPosIndex());
        builder.setUserId(pointInfo.baseInfo.getUserId());
        if(pointInfo.baseInfo.getUserId() > 0){
            builder.setUserId(pointInfo.baseInfo.getUserId());
            UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(pointInfo.baseInfo.getUserId());
            if(userBaseInfo != null){
                builder.setPlayerInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
            }
//            builder.setTotalValues(getTotalValue(pointInfo));
            builder.setBuffValues(pointInfo.baseInfo.getBuffValues());
            builder.setProduceRewardLastTime(pointInfo.baseInfo.getProduceRewardLastTime());
            builder.setStartTime(pointInfo.baseInfo.getSeatTime());
            builder.setEndTime(pointInfo.baseInfo.getEndTime());
        }else {
            builder.setRemainTime(pointInfo.baseInfo.getRemainTime());
        }
        return builder;
    }

    public static MonopolyMarketProto.MonopolyMarketResGrabPointEntity.Builder builderMonopolyMarketResGrabPointEntity(MonopolyMarketResGrabPointInfo pointInfo){
        MonopolyMarketProto.MonopolyMarketResGrabPointEntity.Builder builder = MonopolyMarketProto.MonopolyMarketResGrabPointEntity.newBuilder();
        builder.setPosIndex(pointInfo.baseInfo.getPosIndex());
        if(pointInfo.baseInfo.getUserId() > 0){
            builder.setUserId(pointInfo.baseInfo.getUserId());
            builder.setEndTime(pointInfo.baseInfo.getEndTime());
            UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(pointInfo.baseInfo.getUserId());
            if(userBaseInfo != null){
                builder.setUserName(userBaseInfo.getNickName());
                builder.setUnionId(userBaseInfo.getUnionUid());
            }
        }else {
            builder.setRemainTime(pointInfo.baseInfo.getRemainTime());
        }
        return builder;
    }

    private static MonopolyMarketProto.MonopolyMarketResGrabUserPointEntity.Builder builderMonopolyMarketResGrabUserPointEntity(MonopolyMarketResGrabPointInfo pointInfo){
        MonopolyMarketProto.MonopolyMarketResGrabUserPointEntity.Builder builder = MonopolyMarketProto.MonopolyMarketResGrabUserPointEntity.newBuilder();
        builder.setType(pointInfo.baseInfo.getType());
        builder.setPosIndex(pointInfo.baseInfo.getPosIndex());
        builder.setProduceRewardLastTime(pointInfo.baseInfo.getProduceRewardLastTime());
        builder.setEndTime(pointInfo.baseInfo.getEndTime());
        builder.setTotalValues(getTotalValue(pointInfo));
        builder.setBuffValues(pointInfo.baseInfo.getBuffValues());
        return builder;
    }


    private static void broadcastMonopolyMarketResgrabRespMsg(MonopolyMarketResGrabPointInfo pointInfo,long excludeId ){
        MonopolyMarketProto.BroadcastMonopolyMarketRespMsg.Builder builder = MonopolyMarketProto.BroadcastMonopolyMarketRespMsg.newBuilder();
        builder.setType(3);
        MonopolyMarketProto.MonopolyMarketResGrabPointDetailBroadcastEntity.Builder detailBroadcast = MonopolyMarketProto.MonopolyMarketResGrabPointDetailBroadcastEntity.newBuilder();
        detailBroadcast.setType(pointInfo.baseInfo.getType());
        detailBroadcast.setPointEntity(builderMonopolyMarketResGrabPointDetailEntity(pointInfo));
        builder.setPointInfo(detailBroadcast);

        for(long userId : pointInfo.userSet){
            if(userId != excludeId){
                MessageHelper.sendPacket(CrossUserMgr.getServerIdByUserId(userId), userId, YanQuMessageUtils.buildMessage(ClientProtocol.U_MONOPOLY_MARKET_BROADCAST, builder));
            }
        }
    }

    private static PatronsProto.PatronsTempMsg.Builder parsePatronsTempMsg(MonopolyMarketPatronsInfo patronsInfo) {
        PatronsProto.PatronsTempMsg.Builder msg = PatronsProto.PatronsTempMsg.newBuilder();
        msg.setPatronsId(patronsInfo.getId());
        msg.setLevel(patronsInfo.getLevel());
        msg.setStageLv(0);
        msg.setBreakLv(0);
        msg.setAddAbility("");
        msg.setTradeWarAddEarnSpeed("");
        msg.setSkinId(patronsInfo.getSkinId());
        msg.setOreWarEarnSpeed("");
        msg.setCookExp(0);
        msg.setBaiYeExp(0);
        msg.setChouSuanExp(0);
        return msg;
    }


    /**
     * @param pointInfo
     * @param attackObjectList
     * @return
     */
    private static boolean battle(MonopolyMarketResGrabPointInfo pointInfo,long attackerId,List<IMonopolyMarketObjectInfo> attackObjectList,MonopolyMarketProto.MonopolyMarketResGrabActionRespMsg.Builder msg,int attackBuffValues,MonopolyMarketResConfigData configData,List<IMonopolyMarketObjectInfo> defendObjectList){
        BattleResult battleResult = battle(pointInfo.baseInfo.getUserId(),defendObjectList,pointInfo.baseInfo.getBuffValues(),attackerId,attackObjectList,attackBuffValues);
        //战斗详细信息
        msg.setBattleDetailList(BattlePb.parseBattleDetailListMsg(battleResult.getPatronsBattleDetailList()));
        //对手出战门客、座骑
        if(pointInfo.baseInfo.getType() == eMonopolyMarketResType.RES_KING.getValue()){
            // 门客
            for (IMonopolyMarketObjectInfo item : defendObjectList) {
                //
                msg.addEnemyPatronsList(builderMonopolyMarketPatronsEntity((MonopolyMarketPatronsInfo)item));
            }
        }else if(pointInfo.baseInfo.getType() == eMonopolyMarketResType.RES_QUEEN.getValue()){
            for (IMonopolyMarketObjectInfo item : defendObjectList) {
                //
                msg.addEnemyBeautyList(builderMonopolyMarketBeautyEntity((MonopolyMarketBeautyInfo)item));
            }
        }
        return battleResult.getWinUserId() == attackerId;
    }

    /**
     * 战斗 战斗力一样算 防守方胜利
     */
    public static BattleResult battle(long defenderId, List<IMonopolyMarketObjectInfo> defendObjectList,int defendBuffValue, long attackerId, List<IMonopolyMarketObjectInfo> attackObjectList,int attackBuffValue) {
        BattleResult battleResult = new BattleResult();
        int defendIndex = 0;
        int attackIndex = 0;
        IMonopolyMarketObjectInfo defendObject = null;
        IMonopolyMarketObjectInfo attackObject = null;
        long defendTotalBlood = 0;
        long attackTotalBlood = 0;

        long defendRemainBlood = 0;
        long attackRemainBlood = 0;
        while (true) {
            PatronsBattleDetail patronsBattleDetail;
            if(defendObject == null && defendObjectList.size() - 1 >= defendIndex){
                defendObject = defendObjectList.get(defendIndex);
                defendTotalBlood = defendRemainBlood = calAttackValue(defendObject,defendBuffValue);
            }
            if(attackObject == null && attackObjectList.size() - 1 >= attackIndex){
                attackObject = attackObjectList.get(attackIndex);
                attackTotalBlood = attackRemainBlood = calAttackValue(attackObject,attackBuffValue);
            }
            if(attackObject == null){
                battleResult.setWinUserId(defenderId);
                break;
            }
            if(defendObject == null){
                battleResult.setWinUserId(attackerId);
                break;
            }
            patronsBattleDetail = patronsBattle(defendObject,defendRemainBlood,defendTotalBlood, attackObject,attackRemainBlood,attackTotalBlood);

            battleResult.getPatronsBattleDetailList().add(patronsBattleDetail);
            defendRemainBlood = patronsBattleDetail.getEnemyPatronsCurrentBlood().longValue();
            attackRemainBlood = patronsBattleDetail.getMyPatronsCurrentBlood().longValue();
            if (defendRemainBlood <= 0) {
                defendIndex++;
                defendObject = null;
            }
            if (attackRemainBlood <= 0) {
                attackIndex++;
                attackObject = null;
            }
        }
        battleResult.setKillPatronsCount(defendIndex);
//        battleResult.setEnemyPatronsList(enemyPatronsList);
//        battleResult.setMyPatronsIdList(patronsIdList);

        return battleResult;
    }

    private static PatronsBattleDetail patronsBattle(IMonopolyMarketObjectInfo defender,long defendRemainBlood ,long defendTotalBlood ,IMonopolyMarketObjectInfo attacker,long attackRemainBlood,long attackTotalBlood) {
        PatronsBattleDetail battleDetail = new PatronsBattleDetail();

        long oldAttackRemainBlood = attackRemainBlood;
        long oldDefendRemainBlood = defendRemainBlood;

        attackRemainBlood = oldAttackRemainBlood - oldDefendRemainBlood;
        defendRemainBlood = oldDefendRemainBlood - oldAttackRemainBlood;

        battleDetail.setEnemyPatronsId(defender.getObjectId());
        battleDetail.setEnemyPatronsCurrentBlood(BigInteger.valueOf(defendRemainBlood));
        battleDetail.setEnemyPatronsTotalBlood(BigInteger.valueOf(defendTotalBlood));

        battleDetail.setMyPatronsId(attacker.getObjectId());
        battleDetail.setMyPatronsCurrentBlood(BigInteger.valueOf(attackRemainBlood));
        battleDetail.setMyPatronsTotalBlood(BigInteger.valueOf(attackTotalBlood));

        return battleDetail;
    }


    /**
     * 占领据点
     * @return
     */
    public static void occupyPoint(MonopolyMarketResGrab resGrab,long userId,int now, MonopolyMarketResGrabPointInfo pointInfo,List<IMonopolyMarketObjectInfo> objectInfoList,int buffValues){
        // 对象派遣
        pointInfo.baseInfo.setUserId(userId);
        pointInfo.baseInfo.setSeatTime(now);
        pointInfo.baseInfo.setReceiveLastTime(now);
        pointInfo.baseInfo.setProduceRewardLastTime(now);
        pointInfo.baseInfo.setEndTime(now + pointInfo.baseInfo.getRemainTime());

        pointInfo.baseInfo.getObjectInfoList().clear();
        pointInfo.objectInfoMap.clear();

        pointInfo.baseInfo.setBuffValues(buffValues);
        pointInfo.baseInfo.getObjectInfoList().addAll(objectInfoList);
        for (IMonopolyMarketObjectInfo item : objectInfoList){
            pointInfo.objectInfoMap.put(item.getObjectId(),item);
        }
        pointInfo.baseInfo.setChangeTime(now);
        addCurPointInfo(resGrab,pointInfo);
        synchronized (resGrab.updateAndAddPointInfoSet){
            resGrab.updateAndAddPointInfoSet.add(pointInfo);
        }
    }

    private static void addCurPointInfo(MonopolyMarketResGrab resGrab,MonopolyMarketResGrabPointInfo pointInfo){
        resGrab.getUserPointSet(pointInfo.baseInfo.getUserId(),true).add(pointInfo);
        setMinResGrabEndTime(resGrab,pointInfo);
        resGrab.collectingPointInfoList.add(pointInfo);
    }

    public static void setMinResGrabEndTime(MonopolyMarketResGrab resGrab,MonopolyMarketResGrabPointInfo pointInfo){
        int endTime = getPointEndTime(resGrab,pointInfo);
        if(endTime != -1 && (resGrab.minResGrabEndTime == 0 || endTime < resGrab.minResGrabEndTime)){
            resGrab.minResGrabEndTime = endTime;
            logger.info("下次执行榷场资源定时任务时间为：{},delay:{},groupId:{}",resGrab.minResGrabEndTime,resGrab.minResGrabEndTime - DateHelper.getCurrentSecond(),pointInfo.baseInfo.getGroupId());
        }
    }

    /**
     * 撤回据点
     */
    private static void leavePointInfo(MonopolyMarketResGrab resGrab,MonopolyMarketResGrabPointInfo pointInfo,int endTime){
        Set<MonopolyMarketResGrabPointInfo> set = resGrab.getUserPointSet(pointInfo.baseInfo.getUserId(),true);
        set.remove(pointInfo);
        resGrab.collectingPointInfoList.remove(pointInfo);
        // 收益时间
        int time = endTime - pointInfo.baseInfo.getSeatTime();
        pointInfo.baseInfo.setRemainTime(pointInfo.baseInfo.getRemainTime() - time);
        pointInfo.baseInfo.setUserId(0);
        pointInfo.baseInfo.setSeatTime(0);
        pointInfo.baseInfo.setBuffValues(0);
        pointInfo.baseInfo.setProduceRewardLastTime(0);
        pointInfo.baseInfo.setReceiveLastTime(0);
        pointInfo.baseInfo.getObjectInfoList().clear();
        pointInfo.objectInfoMap.clear();

        pointInfo.baseInfo.setChangeTime(endTime);
        synchronized (resGrab.updateAndAddPointInfoSet){
            resGrab.updateAndAddPointInfoSet.add(pointInfo);
        }
    }

    /**
     * 通知区服加战报
     * @param resGrab
     * @param attackerId
     * @param attackerInfo
     * @param posIndex
     * @param defenderServerId
     * @param defenderId
     * @param isWin
     */
    private static void addResGrabReport(MonopolyMarketResGrab resGrab,long attackerId, UserBaseInfo attackerInfo,int posIndex,long defenderServerId,long defenderId, boolean isWin,int level,int addRankValue){
        MonopolyMarketProto.MonopolyMarketSynAttackToGameServerMsg.Builder builder = MonopolyMarketProto.MonopolyMarketSynAttackToGameServerMsg.newBuilder();
        builder.setType(resGrab.type);
        builder.setRobedId(defenderId);
        builder.setPosIndex(posIndex);
        builder.setAttackId(attackerId);
        if(attackerInfo.getServerId() != defenderServerId){
            builder.setAttackInfo(PlayerBasePb.parsePlayerBaseTempMsg(attackerInfo));
        }
        builder.setIsWin(isWin);
        builder.setLevel(level);
        builder.setX(posIndex % resGrab.map.side + 1);
        builder.setY(posIndex / resGrab.map.side + 1);
        builder.setAddValue(addRankValue);
        MessageHelper.sendPacket(defenderServerId,defenderId, YanQuMessageUtils.buildMessage(GameProtocol.S_MONOPOLY_MARKET_SYN_ATTACK, builder));
    }

    public static List<IMonopolyMarketObjectInfo> formatPatronsList(List<MonopolyMarketProto.MonopolyMarketPatronsEntity> patronsList){
        List<IMonopolyMarketObjectInfo> list = new ArrayList<>();
        for (MonopolyMarketProto.MonopolyMarketPatronsEntity item : patronsList){
            MonopolyMarketPatronsInfo patronsInfo = new MonopolyMarketPatronsInfo();
            patronsInfo.setId(item.getPatronsId());
            patronsInfo.setLevel(item.getLevel());
            patronsInfo.setStageLv(item.getStageLv());
            patronsInfo.setSkinId(item.getSkinId());
            patronsInfo.setEarn(item.getEarn());
            patronsInfo.setAddPlus(item.getAddPlus());
            patronsInfo.setPromotionId(item.getPromotionId());
            list.add(patronsInfo);
        }
        return list;
    }
    public static List<IMonopolyMarketObjectInfo> formatBeautyList(List<MonopolyMarketProto.MonopolyMarketBeautyEntity> beautyList){
        List<IMonopolyMarketObjectInfo> list = new ArrayList<>();
        for (MonopolyMarketProto.MonopolyMarketBeautyEntity item : beautyList){
            MonopolyMarketBeautyInfo patronsInfo = new MonopolyMarketBeautyInfo();
            patronsInfo.setId(item.getId());
            patronsInfo.setValue(item.getValue());
            patronsInfo.setSkinId(item.getSkinId());
            patronsInfo.setAddPlus(item.getAddPlus());
            list.add(patronsInfo);
        }
        return list;
    }

    public static long getTotalValue(MonopolyMarketResGrabPointInfo pointInfo){
        long totalValue = 0;
        for (IMonopolyMarketObjectInfo item : pointInfo.baseInfo.getObjectInfoList()){
            totalValue += item.getValue();
        }
        return totalValue;
    }

    /**
     * 重置据点 (每天重置)
     */
    public static boolean refreshMonopolyMarketResGrab(MonopolyMarket monopolyMarket,int type, int now){
        int lastRefreshTime = 0;
        if(type == eMonopolyMarketResType.RES_KING.getValue()){
            lastRefreshTime = monopolyMarket.group.getKingResGrabRefreshTime();
        }else if(type == eMonopolyMarketResType.RES_QUEEN.getValue()){
            lastRefreshTime = monopolyMarket.group.getQueenResGrabRefreshTime();
        }
        if(DateHelper.getDiffDayNum(lastRefreshTime) != 0 ){
            MonopolyMarketResGrab resGrab = monopolyMarket.monopolyMarketResGrab.get(type);
            if(resGrab != null){
                for (MonopolyMarketResGrabPointInfo pointInfo : resGrab.pointsMap.values()){
                    clearPointInfo(resGrab, pointInfo,true);
                    synchronized (resGrab.updateAndAddPointInfoSet){
                        resGrab.updateAndAddPointInfoSet.add(pointInfo);
                    }
                }
            }
            if(type == eMonopolyMarketResType.RES_KING.getValue()){
                monopolyMarket.group.setKingResGrabRefreshTime(now);
            }else if(type == eMonopolyMarketResType.RES_QUEEN.getValue()){
                monopolyMarket.group.setQueenResGrabRefreshTime(now);
            }
            return true;
        }
        return false;
    }

    /**
     * 领取奖励
     */
    public static MonopolyMarketProto.MonopolyMarketEnterRespMsgToGameServerMsg.Builder receiveReward(long userId){
        MonopolyMarketProto.MonopolyMarketEnterRespMsgToGameServerMsg.Builder builder = MonopolyMarketProto.MonopolyMarketEnterRespMsgToGameServerMsg.newBuilder();
        List<MonopolyMarketProto.MonopolyMarketSynPointSettlementToGameServerMsg> settlementList = receiveRewardList(userId);
        builder.addAllPointSettlementList(settlementList);
        return builder;
    }

    /**
     * @param userId
     * @return
     */
    public static List<MonopolyMarketProto.MonopolyMarketSynPointSettlementToGameServerMsg> receiveRewardList(long userId){
        List<MonopolyMarketProto.MonopolyMarketSynPointSettlementToGameServerMsg> settlementList = new ArrayList<>();
        List<MonopolyMarketResGrabFinishData> list = getMonopolyMarketResGrabFinishDataList(userId,false);
        if(list != null && list.size() > 0){
            List<MonopolyMarketResGrabFinishData> tempList = new ArrayList<>();
            synchronized (list) {
                tempList.addAll(list);
                list.clear();
            }
            // 1 清空 入库
            for (MonopolyMarketResGrabFinishData item : tempList){
                MonopolyMarketProto.MonopolyMarketSynPointSettlementToGameServerMsg.Builder settlement = MonopolyMarketProto.MonopolyMarketSynPointSettlementToGameServerMsg.newBuilder();
                settlement.setType(item.getType());
                settlement.setPosIndex(item.getPosIndex());
                settlement.setSide(item.getSide());
                settlement.setLevel(item.getLevel());
                settlement.setRobName(item.getRobName());
                settlement.setRewardTime(item.getRewardTime());
                settlement.setRewardNum(item.getRewardNum());
                settlement.setTotalRewardNum(item.getTotalRewardNum());
                settlement.addAllDispatchObjectId(StringUtils.stringToIntegerList(item.getObjectStr(),","));
                settlement.setBuffValues(item.getBuffValues());
                item.setIsReceive(true);
                settlementList.add(settlement.build());
            }
            // 入库
            synchronized (tempResGrabFinishList){
                tempResGrabFinishList.addAll(tempList);
            }
        }
        return settlementList;
    }

    /**
     * 尝试结算
     */
    public static boolean attemptSettlement(MonopolyMarket monopolyMarket,MonopolyMarketResGrab resGrab,MonopolyMarketResGrabPointInfo pointInfo,int now){
        int endTime = getPointEndTime(resGrab,pointInfo);
        if(endTime != -1 && endTime <= now){
            normalSettlement(monopolyMarket,resGrab,pointInfo,false,endTime);
            return true;
        }
        return false;
    }


    public static void exitPoint(MonopolyMarket monopolyMarket,int type,int posIndex,long userId){
        MonopolyMarketResGrab resGrab = monopolyMarket.monopolyMarketResGrab.get(type);
        if(resGrab != null){
            MonopolyMarketResGrabPointInfo pointInfo = resGrab.pointsMap.get(posIndex);
            if(pointInfo != null){
                pointInfo.userSet.remove(userId);
                resGrab.userInPointMap.remove(userId);
            }
        }
    }

    /**
     * 计算资源点结算的时间  -1表示不能结算
     */
    public static int getPointEndTime(MonopolyMarketResGrab resGrab,MonopolyMarketResGrabPointInfo item){
        if(item.baseInfo.getUserId() > 0){
            int endTime = calDailyEndTime(resGrab.type,item.baseInfo.getSeatTime());
            return Math.min(endTime,item.baseInfo.getEndTime());
        }
        return -1;
    }


    /**
     * 计算当前时间的资源点 当天结算时间
     * @param type
     * @param startTime
     * @return
     */
    private static int calDailyEndTime(int type,int startTime){
        // 当前结算时间
        int todayStartTime = DateHelper.getZeroTime(startTime);
        if(eMonopolyMarketResType.RES_KING.getValue() == type){
            return todayStartTime + ConfigMgr.getKingDailyEndTime();
        }else if(eMonopolyMarketResType.RES_QUEEN.getValue() == type){
            return todayStartTime + ConfigMgr.getQueenDailyEndTime();
        }
        return 0;
    }

    public static List<MonopolyMarketResGrabFinishData> getMonopolyMarketResGrabFinishDataList(long userId,boolean newIfNull) {
        List<MonopolyMarketResGrabFinishData> list = userResGrabFinishMap.get(userId);
        if (list == null && newIfNull) {
            synchronized (userResGrabFinishMap){
                if(!userResGrabFinishMap.containsKey(userId)){
                    list = new ArrayList<>();
                    userResGrabFinishMap.put(userId, list);
                }
            }
        }
        return list;
    }

    /**
     * 队伍数据
     * @param pointInfo
     * @return
     */
    public static MonopolyMarketProto.MonopolyMarketGetTeamEntity.Builder builderMonopolyMarketGetTeamEntity(MonopolyMarketResGrabPointInfo pointInfo){
        MonopolyMarketProto.MonopolyMarketGetTeamEntity.Builder team = MonopolyMarketProto.MonopolyMarketGetTeamEntity.newBuilder();
        team.setType(pointInfo.baseInfo.getType());
        team.setPosIndex(pointInfo.baseInfo.getPosIndex());
        team.setEndTime(pointInfo.baseInfo.getEndTime());
        team.setStartTime(pointInfo.baseInfo.getSeatTime());
        team.setReceiveLastTime(pointInfo.baseInfo.getReceiveLastTime());
        team.setProduceRewardLastTime(pointInfo.baseInfo.getProduceRewardLastTime());
        for (IMonopolyMarketObjectInfo objectInfo : pointInfo.baseInfo.getObjectInfoList()){
            team.addObjectList(objectInfo.getObjectId());
        }
        team.setBuffValues(pointInfo.baseInfo.getBuffValues());
        return team;
    }


    public static MonopolyMarketProto.MonopolyMarketAddRankValueEntity.Builder builderMonopolyMarketAddRankValueEntity(MonopolyMarketResGrabReceiveResult receiveResult){
        MonopolyMarketProto.MonopolyMarketAddRankValueEntity.Builder builder = MonopolyMarketProto.MonopolyMarketAddRankValueEntity.newBuilder();
        builder.setAddValue(receiveResult.getAddValue());
        builder.setStartTime(receiveResult.getStartTime());
        builder.setTime(receiveResult.getTime());
        builder.setType(receiveResult.getType());
        return builder;
    }

    public static MonopolyMarketProto.MonopolyMarketPatronsEntity.Builder builderMonopolyMarketPatronsEntity(MonopolyMarketPatronsInfo patronsInfo){
        MonopolyMarketProto.MonopolyMarketPatronsEntity.Builder builder = MonopolyMarketProto.MonopolyMarketPatronsEntity.newBuilder();
        builder.setPatronsId(patronsInfo.getId());
        builder.setLevel(patronsInfo.getLevel());
        builder.setStageLv(patronsInfo.getStageLv());
        builder.setSkinId(patronsInfo.getSkinId());
        builder.setEarn(patronsInfo.getEarn());
        builder.setAddPlus(patronsInfo.getAddPlus());
        builder.setPromotionId(patronsInfo.getPromotionId());
        return builder;
    }

    public static MonopolyMarketProto.MonopolyMarketBeautyEntity.Builder builderMonopolyMarketBeautyEntity(MonopolyMarketBeautyInfo beautyInfo){
        MonopolyMarketProto.MonopolyMarketBeautyEntity.Builder builder = MonopolyMarketProto.MonopolyMarketBeautyEntity.newBuilder();
        builder.setId(beautyInfo.getId());
        builder.setSkinId(beautyInfo.getSkinId());
        builder.setValue(beautyInfo.getValue());
        builder.setAddPlus(beautyInfo.getAddPlus());
        return builder;
    }


    public static MonopolyMarketProto.MonopolyMarketResGrabSearchPointEntity.Builder builderMonopolyMarketResGrabSearchPointEntity(MonopolyMarketResGrabPointInfo pointInfo){
        MonopolyMarketProto.MonopolyMarketResGrabSearchPointEntity.Builder builder = MonopolyMarketProto.MonopolyMarketResGrabSearchPointEntity.newBuilder();
        builder.setPosIndex(pointInfo.baseInfo.getPosIndex());
        if(pointInfo.baseInfo.getUserId() > 0){
            builder.setUserId(pointInfo.baseInfo.getUserId());
            builder.setEndTime(pointInfo.baseInfo.getEndTime());
            UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(pointInfo.baseInfo.getUserId());
            if(userBaseInfo != null){
                builder.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
            }
        }else {
            builder.setRemainTime(pointInfo.baseInfo.getRemainTime());
        }
        return builder;
    }


    /**
     * 强制结算所有据点
     */
    public static void forceSettlement(MonopolyMarket monopolyMarket,MonopolyMarketResGrab resGrab){
        int now = DateHelper.getCurrentSecond();
        Iterator<MonopolyMarketResGrabPointInfo> iterator = resGrab.collectingPointInfoList.iterator();
        while (iterator.hasNext()){
            MonopolyMarketResGrabPointInfo item = iterator.next();
            iterator.remove();
            normalSettlement(monopolyMarket,resGrab,item,false,now);
        }
        resGrab.collectingPointInfoList.clear();
    }


    /**
     * 是否重复派遣
     * @return
     */
    private static boolean isRepeatDispatch(MonopolyMarketResGrab resGrab,long userId, List<IMonopolyMarketObjectInfo> list){
        Set<MonopolyMarketResGrabPointInfo> set = resGrab.getUserPointSet(userId,false);
        if(set == null){
            return false;
        }
        for (MonopolyMarketResGrabPointInfo item : set){
            if(item.baseInfo.getUserId() != userId){
                continue;
            }
            for (IMonopolyMarketObjectInfo info : list){
                if(item.objectInfoMap.containsKey(info.getObjectId())){
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 计算总战斗力
     * @param objectInfos
     * @param buffValues
     * @return
     */
    private static long calPointValue(List<IMonopolyMarketObjectInfo> objectInfos,int buffValues){
        long value = 0;
        for(IMonopolyMarketObjectInfo objectInfo : objectInfos){
            value += calAttackValue(objectInfo,buffValues);
        }
        return value;
    }


    /**
     * 计算派遣对象的战斗力
     * @param objectInfo
     * @param buffValue buff 加成
     * @return
     */
    private static long calAttackValue(IMonopolyMarketObjectInfo objectInfo, int buffValue){
        return (objectInfo.getValue() * (1000 + objectInfo.getAddPlus()) / 1000 * (1000 + buffValue) + 1000 - 1)/ 1000;
    }

    /**
     * 拼接派遣对象的id
     * @param objectInfoList
     * @return
     */
    private static String getObjectStr(List<IMonopolyMarketObjectInfo> objectInfoList){
        StringBuilder sb = new StringBuilder();
        for (IMonopolyMarketObjectInfo objectInfo : objectInfoList){
            sb.append(objectInfo.getObjectId()).append(",");
        }
        if(sb.length() > 0){
            return sb.substring(0,sb.length()-1);
        }
        return "";
    }

    /**
     * 获取对应配置的npc 信息
     * @param monopolyMarket
     * @param configData
     */
    private static List<IMonopolyMarketObjectInfo> getNpcObjectList(MonopolyMarket monopolyMarket, MonopolyMarketResConfigData configData){
        List<IMonopolyMarketObjectInfo> npcList = new ArrayList<>();
        String npcStr = MonopolyMarketCommonMgr.getNpcStr(monopolyMarket.group.getServerList(),MonopolyMarketMgr.crossConfig ,configData);
        String[] npcStrList = npcStr.split("\\|");
        for (String item : npcStrList){
            String[] objectInfo = item.split(";");
            if(configData.getType() == eMonopolyMarketResType.RES_KING.getValue()){
                MonopolyMarketPatronsInfo patronsInfo = new MonopolyMarketPatronsInfo();
                patronsInfo.setId(Integer.valueOf(objectInfo[0]));
                patronsInfo.setEarn(Long.valueOf(objectInfo[1]));
                npcList.add(patronsInfo);
            }else if(configData.getType() == eMonopolyMarketResType.RES_QUEEN.getValue()) {
                MonopolyMarketBeautyInfo beautyInfo = new MonopolyMarketBeautyInfo();
                beautyInfo.setId(Integer.valueOf(objectInfo[0]));
                beautyInfo.setValue(Long.valueOf(objectInfo[1]));
                npcList.add(beautyInfo);
            }
        }
        return npcList;
    }

    /**
     * 获取玩家已有的据点
     * @return
     */
    private static String getCurUserPoint(MonopolyMarketResGrab resGrab,long userId){
        Set<MonopolyMarketResGrabPointInfo> set = resGrab.getUserPointSet(userId,false);
        if(set != null){
            StringBuilder sb = new StringBuilder();
            for (MonopolyMarketResGrabPointInfo item : set){
                if(item.baseInfo.getUserId() != userId){
                    continue;
                }
                sb.append(item.baseInfo.getPosIndex()).append(",");
            }
            if(sb.length() > 0){
                return sb.substring(0,sb.length()-1);
            }
        }
        return "";
    }




}
